//
//  XHQScacleImageView.m
//  KLK
//
//  Created by 帝云科技 on 2018/6/25.
//  Copyright © 2018年 diyunkeji. All rights reserved.
//

#import "XHQScacleImageView.h"

#define Screen_W [[UIScreen mainScreen] bounds].size.width
#define Screen_H  [[UIScreen mainScreen] bounds].size.height

@interface XHQScacleImageView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, assign) CGRect oldFrame;

@end

@implementation XHQScacleImageView

+ (void)xhq_showBigImage:(UIImageView *)imageView
{
    XHQScacleImageView *scacleView = [[XHQScacleImageView alloc]init];
    scacleView.oldFrame = [imageView convertRect:imageView.bounds toView:scacleView];
    scacleView.image = imageView.image;
    [scacleView show];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    if (self = [super initWithFrame:frame])
    {
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    _scrollView = ({
        UIScrollView *s = [[UIScrollView alloc] initWithFrame:self.bounds];
        s.showsVerticalScrollIndicator = NO;
        s.showsHorizontalScrollIndicator = NO;
        s.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        s.minimumZoomScale = 0.5f;
        s.maximumZoomScale = 2.0f;
        s.bounces = NO;
        s.delegate = self;
        s;
    });
    _imageView = ({
        UIImageView *iv = [[UIImageView alloc] init];
        iv.contentMode = UIViewContentModeScaleAspectFit;
        iv.userInteractionEnabled = YES;
        [iv addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMethod)]];
        iv;
    });
    
    [self addSubview:self.scrollView];
    [self.scrollView addSubview:self.imageView];
}

- (void)show
{
    UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
    [keyWindow addSubview:self];
    self.imageView.frame = self.oldFrame;
    self.imageView.image = self.image;
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    [UIView animateWithDuration:0.3 animations:^{
        self.imageView.frame = self.scrollView.bounds;
        self.scrollView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:1];
    }];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.3 animations:^{
        self.imageView.frame = self.oldFrame;
        self.scrollView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
    }];
}

- (void)tapMethod
{
   [self dismiss];
}

#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    NSLog(@"scrollView: %@", NSStringFromCGSize(self.scrollView.contentSize));
    NSLog(@"imageView: %@", NSStringFromCGRect(self.imageView.frame));
    
    CGRect frame = self.imageView.frame;
    
    frame.origin.y = (self.scrollView.frame.size.height - self.imageView.frame.size.height) > 0 ? (self.scrollView.frame.size.height - self.imageView.frame.size.height) * 0.5 : 0;
    frame.origin.x = (self.scrollView.frame.size.width - self.imageView.frame.size.width) > 0 ? (self.scrollView.frame.size.width - self.imageView.frame.size.width) * 0.5 : 0;
    self.imageView.frame = frame;
    
    self.scrollView.contentSize = CGSizeMake(self.imageView.frame.size.width + 30, self.imageView.frame.size.height + 30);
}

@end
